# 打开文件C:\\Users\\Administrator\\Downloads\\喜马拉雅.csv，并以读写模式打开
# 读取文件中的所有行，并将它们存储在变量lines中
# 定义文件第一列为时间，第二列为放款金额，第三列为还款金额，第四列为当日新增贷余，第五列为当日贷余
# 第五列需要滚动计算，当日贷余 = 当日新增贷余 + 历史贷余
# 历史贷余就是前一天的当日贷余

def listToStr(list):
    return ','.join(list)


def yanfaniandu(file1, file2):
    # 打开文件D:\\喜马拉雅选项目.csv，并以读写模式打开
    with open(file2, "w", encoding='utf-8') as f2:
        with open(file1, "rb") as f:
            lines = f.readlines()
            # 初始化历史贷余为0
            history_loan_balance = 0
            # 遍历文件中的每一行
            for i, line in enumerate(lines):
                # 如果是第一行，跳过\
                line_list = line.decode().strip().split(',')
                if i == 0:
                    line_list.append('当日贷余')
                    f2.write(','.join(line_list) + '\n')
                    continue
                # 如果行中有5列数据
                if len(line_list) == 4:
                    # 计算当日贷余
                    line_list[3] = line_list[3].replace('\'', '')
                    current_loan_balance = float(line_list[3]) + history_loan_balance
                    # 更新历史贷余
                    history_loan_balance = current_loan_balance
                    # 将当日贷余写入文件
                    line_list.append(str(current_loan_balance))
                    f2.write(','.join(line_list) + '\n')


def dayToMonth(file, file2):
    with open(file2, "w", encoding='utf-8') as f2:
        # 打开文件D:\\喜马拉雅项目.csv，并以读写模式打开
        with open(file, "rb") as f:
            lines = f.readlines()
            # 遍历文件中的每一行
            newlinses = []
            ii = 0
            month = ''
            newAmount = 0
            newRemain = 0
            inline12 = {'month': None, 'newAmount': 0, 'newRemain': 0}
            inline1212 = []
            for i, line in enumerate(lines):
                inline = []
                line_list = line.decode().strip().split(',')
                # 如果是第一行，跳过
                if i == 0:
                    inline.append('月份')
                    inline.append('新增')
                    inline.append('日贷余累计')
                    newlinses.append(inline)
                    continue
                if len(line_list) == 5:
                    nmonth = line_list[0][0:7]
                    if nmonth == month:
                        newAmount = newAmount + float(line_list[1])
                        newRemain = newRemain + float(line_list[4])

                        if month == '2025-01':
                            inline12['month'] = month
                            inline12['newAmount'] = newAmount
                            inline12['newRemain'] = newRemain
                    else:
                        if ii > 0:
                            inline.append(str(month))
                            inline.append(str(newAmount / 100 / 10000))
                            inline.append(str(newRemain / 100 / 10000))
                            newlinses.append(inline)

                        month = nmonth
                        newAmount = float(line_list[1])
                        newRemain = float(line_list[4])
                        ii = ii + 1
            month_ = inline12['month']
            if month_ is not None:
                inline1212.append(str(inline12['month']))
                inline1212.append(str(inline12['newAmount'] / 100 / 10000))
                inline1212.append(str(inline12['newRemain'] / 100 / 10000))
                newlinses.append(inline1212)
        for line in newlinses:
            f2.write((','.join(line) + '\n'))


if __name__ == '__main__':
    yanfaniandu("D:\\data\\yanfacesuan\\yanfa2\\小米新鼎和项目.csv",
                "D:\\data\\yanfacesuan\\yanfa2\\小米新鼎和项目-结果-日.csv")
    dayToMonth("D:\\data\\yanfacesuan\\yanfa2\\小米新鼎和项目-结果-日.csv",
               "D:\\data\\yanfacesuan\\yanfa2\\小米新鼎和项目-结果-月.csv")
    print("小米新鼎和")
    yanfaniandu("D:\\data\\yanfacesuan\\yanfa2\\小米信和项目.csv",
                "D:\\data\\yanfacesuan\\yanfa2\\小米信和项目-结果-日.csv")
    dayToMonth("D:\\data\\yanfacesuan\\yanfa2\\小米信和项目-结果-日.csv",
               "D:\\data\\yanfacesuan\\yanfa2\\小米信和项目-结果-月.csv")
    print("小米信和")
    yanfaniandu("D:\\data\\yanfacesuan\\yanfa2\\小米鸿耀项目.csv",
                "D:\\data\\yanfacesuan\\yanfa2\\小米鸿耀项目-结果-日.csv")
    dayToMonth("D:\\data\\yanfacesuan\\yanfa2\\小米鸿耀项目-结果-日.csv",
               "D:\\data\\yanfacesuan\\yanfa2\\小米鸿耀项目-结果-月.csv")
    print("小米鸿耀")

    yanfaniandu("D:\\data\\yanfacesuan\\yanfa2\\流量项目.csv", "D:\\data\\yanfacesuan\\yanfa2\\流量项目-结果-日.csv")
    dayToMonth("D:\\data\\yanfacesuan\\yanfa2\\流量项目-结果-日.csv",
               "D:\\data\\yanfacesuan\\yanfa2\\流量项目-结果-月.csv")
    print("流量")

    yanfaniandu("D:\\data\\yanfacesuan\\yanfa2\\度小满贷超项目.csv",
                "D:\\data\\yanfacesuan\\yanfa2\\度小满贷超项目-结果-日.csv")
    dayToMonth("D:\\data\\yanfacesuan\\yanfa2\\度小满贷超项目-结果-日.csv",
               "D:\\data\\yanfacesuan\\yanfa2\\度小满贷超项目-结果-月.csv")
    print("度小满贷超")
    yanfaniandu("D:\\data\\yanfacesuan\\yanfa2\\度小满辅助项目.csv",
                "D:\\data\\yanfacesuan\\yanfa2\\度小满辅助项目-结果-日.csv")
    dayToMonth("D:\\data\\yanfacesuan\\yanfa2\\度小满辅助项目-结果-日.csv",
               "D:\\data\\yanfacesuan\\yanfa2\\度小满辅助项目-结果-月.csv")
    print("度小满辅助")

    yanfaniandu("D:\\data\\yanfacesuan\\yanfa2\\昆仑桔子项目.csv",
                "D:\\data\\yanfacesuan\\yanfa2\\昆仑桔子项目-结果-日.csv")
    dayToMonth("D:\\data\\yanfacesuan\\yanfa2\\昆仑桔子项目-结果-日.csv",
               "D:\\data\\yanfacesuan\\yanfa2\\昆仑桔子项目-结果-月.csv")
    print("昆仑桔子")
    yanfaniandu("D:\\data\\yanfacesuan\\yanfa2\\昆仑还呗项目.csv",
                "D:\\data\\yanfacesuan\\yanfa2\\昆仑还呗项目-结果-日.csv")
    dayToMonth("D:\\data\\yanfacesuan\\yanfa2\\昆仑还呗项目-结果-日.csv",
               "D:\\data\\yanfacesuan\\yanfa2\\昆仑还呗项目-结果-月.csv")
    print("昆仑还呗")
    yanfaniandu("D:\\data\\yanfacesuan\\yanfa2\\昆仑洋钱罐项目.csv",
                "D:\\data\\yanfacesuan\\yanfa2\\昆仑洋钱罐项目-结果-日.csv")
    dayToMonth("D:\\data\\yanfacesuan\\yanfa2\\昆仑洋钱罐项目-结果-日.csv",
               "D:\\data\\yanfacesuan\\yanfa2\\昆仑洋钱罐项目-结果-月.csv")
    print("昆仑洋钱罐")
